package com.zitangkou.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zitangkou.common.enums.RespEnum;
import com.zitangkou.common.global.GlobalResp;
import com.zitangkou.user.info.UserInfo;
import com.zitangkou.user.local.UserThreadLocal;
import com.zitangkou.user.req.user.*;
import com.zitangkou.user.resp.user.ConstantListResp;
import com.zitangkou.user.resp.user.UserLoginResp;
import com.zitangkou.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户模块
 *
 * @author Deng Ningning
 * @version 1.0
 * @date 2020/8/31 16:39
 */

@Slf4j
@RestController
@RequestMapping("/api/v1/users")
public class UserController {

    private final IUserService userService;

    @Autowired
    public UserController(IUserService userService) {
        this.userService = userService;
    }

    /**
     * 分页条件查询用户
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp<com.baomidou.mybatisplus.core.metadata.IPage < com.zitangkou.user.model.info.UserInfo>>
     * @date 2021/2/27 13:20
     */
    @GetMapping
    public GlobalResp<IPage<UserInfo>> selectByPage(
            @Validated GetUserByPageReq req) {
        return GlobalResp.success(userService.getUserPage(req, UserThreadLocal.getUserInfo()));
    }

    /**
     * 用户账号登录
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp<com.zitangkou.user.model.resp.user.UserLoginResp>
     * @date 2021/2/27 13:26
     */
    @PostMapping("/login/account")
    public GlobalResp<UserLoginResp> accountLogin(
            @RequestBody @Validated AccountLoginReq req) {
        return GlobalResp.success(userService.accountLogin(req, UserThreadLocal.getUserInfo()));
    }

    /**
     * 手机号码登录
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp<com.zitangkou.user.model.resp.user.UserLoginResp>
     * @author Deng Ningning
     * @date 2020/9/12 16:00
     */
    @PostMapping("/login/mobile")
    public GlobalResp<UserLoginResp> mobileLogin(
            @RequestBody @Validated MobileLoginReq req) {
        return GlobalResp.success(userService.mobileLogin(req, UserThreadLocal.getUserInfo()));
    }

    /**
     * 邮箱登录
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp<com.zitangkou.user.model.resp.user.UserLoginResp>
     * @author Deng Ningning
     * @date 2020/9/13 13:43
     */
    @PostMapping("/login/email")
    public GlobalResp<UserLoginResp> emailLogin(
            @RequestBody @Validated EmailLoginReq req) {
        return GlobalResp.success(userService.emailLogin(req, UserThreadLocal.getUserInfo()));
    }

    /**
     * 用户注册
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp<com.zitangkou.user.model.resp.user.UserLoginResp>
     * @author Deng Ningning
     * @date 2020/9/3 10:35
     */
    @PostMapping("/register/mobile")
    public GlobalResp<String> mobileRegister(
            @RequestBody @Validated MobileRegisterReq req) {
        return userService.mobileRegister(req, UserThreadLocal.getUserInfo())
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.MOBILE_REGISTER_FAIL);
    }

    /**
     * 用户邮箱注册
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/11 22:34
     */
    @PostMapping("/register/email")
    public GlobalResp<String> emailRegister(
            @RequestBody @Validated EmailRegisterReq req) {
        return userService.emailRegister(req, UserThreadLocal.getUserInfo())
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.EMAIL_REGISTER_FAIL);
    }

    /**
     * 发送短信验证码
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/3 11:38
     */
    @PostMapping("/mobile/code")
    public GlobalResp<Boolean> sendMobileCode(
            @RequestBody @Validated SendMobileCodeReq req) {
        return GlobalResp.success(userService.sendMobileCode(req, UserThreadLocal.getUserInfo()));
    }

    /**
     * 发送邮箱验证码
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/11 22:14
     */
    @PostMapping("/email/code")
    public GlobalResp<Boolean> sendEmailCode(
            @RequestBody @Validated SendEmailCodeReq req) {
        return GlobalResp.success(userService.sendEmailCode(req, UserThreadLocal.getUserInfo()));
    }

    /**
     * 添加用户
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:51
     */
    @PostMapping
    public GlobalResp<String> insertUser(
            @RequestBody @Validated AddUserReq req) {
        return userService.insertUser(req)
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.ADD_DEPARTMENT_FAIL);
    }

    /**
     * 修改用户
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:53
     */
    @PutMapping
    public GlobalResp<String> updateUser(
            @RequestBody @Validated EditUserReq req) {
        return userService.updateUser(req)
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.MODIFY_DEPARTMENT_FAIL);
    }

    /**
     * 绑定手机号码
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/16 22:53
     */
    @PutMapping("/bind/mobile")
    public GlobalResp<String> bindMobile(
            @RequestBody @Validated MobileLoginReq req) {
        return userService.bindMobile(req, UserThreadLocal.getUserInfo())
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.BIND_MOBILE_FAIL);
    }

    /**
     * 绑定邮箱
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/16 23:32
     */
    @PutMapping("/bind/email")
    public GlobalResp<String> bindEmail(
            @RequestBody @Validated EmailLoginReq req) {
        return userService.bindEmail(req, UserThreadLocal.getUserInfo())
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.BIND_EMAIL_FAIL);
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:55
     */
    @DeleteMapping("/{userId}")
    public GlobalResp<String> deleteUser(
            @PathVariable String userId) {
        return userService.deleteUser(userId)
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.DELETE_DEPARTMENT_FAIL);
    }

    /**
     * 查询用户详情
     *
     * @param userId
     * @return com.zitangkou.common.global.GlobalResp<com.zitangkou.user.model.entity.UserDO>
     * @author Deng Ningning
     * @date 2020/9/2 11:56
     */
    @GetMapping("/{userId}")
    public GlobalResp<UserInfo> selectById(
            @PathVariable String userId) {
        return GlobalResp.success(userService.getUserById(userId));
    }

    /**
     * 判断账号是否存在
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp<java.lang.Boolean>
     * @author Deng Ningning
     * @date 2020/9/17 16:51
     */
    @PostMapping("/exist/account")
    public GlobalResp<Boolean> accountExist(@RequestBody @Validated AccountExistReq req) {
        return GlobalResp.success(userService.accountExist(req, UserThreadLocal.getUserInfo()));
    }

    /**
     * 判断手机号码是否存在
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp<java.lang.Boolean>
     * @author Deng Ningning
     * @date 2020/9/17 16:57
     */
    @PostMapping("/exist/mobile")
    public GlobalResp<Boolean> mobileExist(@RequestBody @Validated MobileExistReq req) {
        return GlobalResp.success(userService.mobileExist(req, UserThreadLocal.getUserInfo()));
    }

    /**
     * 判断邮箱是否存在
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp<java.lang.Boolean>
     * @author Deng Ningning
     * @date 2020/9/17 16:57
     */
    @PostMapping("/exist/email")
    public GlobalResp<Boolean> emailExist(@RequestBody @Validated EmailExistReq req) {
        return GlobalResp.success(userService.emailExist(req, UserThreadLocal.getUserInfo()));
    }

    /**
     * 查询常量
     *
     * @param type
     * @return com.zitangkou.common.global.GlobalResp<java.lang.Boolean>
     * @author Deng Ningning
     * @date 2020/9/29 20:18
     */
    @PostMapping("/constants/{type}")
    public GlobalResp<ConstantListResp> getConstants(@PathVariable String type) {
        return GlobalResp.success(userService.getConstants(type));
    }

    /**
     * 查询当前用户
     *
     * @return com.zitangkou.common.global.GlobalResp<top.zitangkou.user.model.entity.UserDO>
     * @author Deng Ningning
     * @date 2020/9/2 11:56
     */
    @GetMapping("/info")
    public GlobalResp<UserLoginResp> getUserInfo() {
        return GlobalResp.success(userService.getUserInfo(UserThreadLocal.getUserInfo()));
    }

    /**
     * 退出登录
     *
     * @return top.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/10/30 13:36
     */
    @GetMapping("/login/out")
    public GlobalResp<Boolean> loginOut() {
        return GlobalResp.success(userService.loginOut(UserThreadLocal.getUserInfo()));
    }

    /**
     * 修改密码
     *
     * @param req
     * @return top.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/16 23:32
     */
    @PutMapping("/forgot/password")
    public GlobalResp<String> forgotPassword(
            @RequestBody @Validated ForgotPasswordReq req) {
        return userService.forgotPassword(req, UserThreadLocal.getUserInfo())
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.FORGOT_PASSWORD_FAIL);
    }

}
